/* -- Include the precompiled libraries -- */

#include <main.h>
#include <extra/rgView.h>
#include <extra/rgEvents.h>

#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

#include "SDL.h"

#include <graphics/rgIntersection.h>

int main(int argc, char **argv)
{
	argc = 0;
	argv = 0;

	rgMatrix4 w2c;
	w2c.setIdentity();
	rgReal nearPlane = 10;
	rgReal farPlane = 10240;
	rgReal fov = 90;
	rgInt xRes = 1024;
	rgInt yRes = 768;

	rgCamera cam(w2c, nearPlane, farPlane, fov, xRes, yRes);

	SDL_Init ( SDL_INIT_VIDEO ) ;
	rgView view(xRes, yRes);

    SDL_Event theEvent ;

    rgBool done = rgFALSE;
    while (!done)
    {
        if ( SDL_PollEvent ( &theEvent ) )
		{
            if ( theEvent.type == SDL_QUIT ) done = rgTRUE ;

			if( theEvent.type == SDL_KEYDOWN)
			{
			    if( theEvent.key.keysym.sym == SDLK_ESCAPE)
                {
				    done = rgTRUE;
                }
            }

            // the triangle
            rgVector t[3] ={rgVector(1024/2, 0, 0), rgVector (0, 767, 0), rgVector (1023, 767, 0)};
            for (unsigned i=0;i<3;i++)
            {
                view.DrawPixel( 10, 255, 255, t[i].x, t[i].y);
            }

            // the intersection tests
            rgVector rayD (0,0,1);
            for (unsigned i=0; i<1023; i=i+5)
            {
                for (unsigned j=0; j<768; j=j+5)
                {
                    rgVector rayO (i,j,-10);
                    rgVector pointOfIntersex;

                    if (rayTriTestPoint( t[0], t[1], t[2], rayO, rayD, pointOfIntersex))
                    {
                        view.DrawPixel( 255, 255, 255, pointOfIntersex.x, pointOfIntersex.y);
                    }
                    else
                    {
                        view.DrawPixel( 255, 0, 0, rayO.x, rayO.y);
                    }
                }
            }
        }
    }

	SDL_Quit( );

	return 0;
}
